# your_file.py

from db_connector import connector
import re
from file_utils import list_files ,calculate_md5,create_folder,get_pdf_page_count

#根据文件明生成id
def extract_info_from_filename(filename):
    # 匹配年份和期数的正则表达式
    pattern1 = re.compile(r'《勇罕》(\d{4})年第(\d+)—(\d+)期总\d+期\.pdf')
    pattern2 = re.compile(r'《勇罕》(\d{4})年第(\d+)期总\d+期\.pdf')

    # 使用正则表达式进行匹配
    match1 = pattern1.match(filename)
    match2 = pattern2.match(filename)

    if match1:
        # 获取年份和期数的匹配组
        year = match1.group(1)
        issue_start = match1.group(2)
        issue_end = match1.group(3)

        # return {
        #     'Year': year,
        #     # 'Issue Range': f"第{issue_start}期—第{issue_end}期"
        #     'Issue Range': issue_start
        # }
        return year+'00'+issue_start
    elif match2:
        # 获取年份和期数的匹配组
        year = match2.group(1)
        issue = match2.group(2)

        return year+'00'+issue
    else:
        return None


if __name__ == "__main__":
    #链接数据库
    # result = connector.execute_query("SELECT * FROM catalog_book")
    # data_list = [{'id': row[0], 'title': row[1]} for row in result]
    # print(result)

    # 获取文件明
    # folder_path = r"D:\Users\65720\Desktop\电信项目-方案\文艺\factory-2"
    # file_names = list_files(folder_path)
    # print(file_names)

   filename1 = "《勇罕》1987年第1—2期总25期.pdf"
   result1 = extract_info_from_filename(filename1)

   filename2 = "《勇罕》2012年第2期总122期.pdf"
   result2 = extract_info_from_filename(filename2)

   if result1:
        print(result1)
   else:
        print("No match found for filename1.")
   if result2:
        print(result2)
   else:
        print("No match found for filename2.")